"""
Admin blueprint views for the Flask Blueprints demo.
"""
from flask import render_template, request, redirect, url_for, flash
from . import admin


# Sample data for demonstration
users_data = [
    {'id': 1, 'username': 'alice', 'email': 'alice@example.com', 'role': 'admin', 'active': True},
    {'id': 2, 'username': 'bob', 'email': 'bob@example.com', 'role': 'editor', 'active': True},
    {'id': 3, 'username': 'charlie', 'email': 'charlie@example.com', 'role': 'user', 'active': True},
    {'id': 4, 'username': 'dave', 'email': 'dave@example.com', 'role': 'user', 'active': False},
    {'id': 5, 'username': 'eve', 'email': 'eve@example.com', 'role': 'user', 'active': True}
]


@admin.route('/')
def dashboard():
    """Admin dashboard."""
    return render_template('admin/dashboard.html')


@admin.route('/users')
def users():
    """User management page."""
    return render_template('admin/users.html', users=users_data)


@admin.route('/posts')
def posts():
    """Post management page."""
    # In a real app, this would query the database for posts
    posts = [
        {'id': 1, 'title': 'First Post', 'author': 'Alice', 'status': 'Published', 'date': '2023-01-01'},
        {'id': 2, 'title': 'Second Post', 'author': 'Bob', 'status': 'Draft', 'date': '2023-01-02'},
        {'id': 3, 'title': 'Third Post', 'author': 'Charlie', 'status': 'Published', 'date': '2023-01-03'}
    ]
    return render_template('admin/base.html', posts=posts)


@admin.route('/settings')
def settings():
    """Site settings page."""
    return render_template('admin/base.html')


@admin.route('/user/<int:user_id>/edit')
def edit_user(user_id):
    """Edit a user."""
    # Find the user with the given ID
    user = next((u for u in users_data if u['id'] == user_id), None)
    
    if user is None:
        flash('User not found.', 'error')
        return redirect(url_for('admin.users'))
    
    # In a real app, this would show a form to edit the user
    flash(f'Editing user {user["username"]}', 'info')
    return redirect(url_for('admin.users'))


@admin.route('/user/create', methods=['POST'])
def create_user():
    """Create a new user."""
    username = request.form.get('username')
    email = request.form.get('email')
    role = request.form.get('role', 'user')
    
    if username and email:
        # In a real app, you would save this to a database
        flash(f'User {username} created successfully!', 'success')
    else:
        flash('Please fill in all fields.', 'error')
    
    return redirect(url_for('admin.users'))


@admin.route('/user/<int:user_id>/activate')
def activate_user(user_id):
    """Activate a user."""
    # Find the user with the given ID
    user = next((u for u in users_data if u['id'] == user_id), None)
    
    if user is None:
        flash('User not found.', 'error')
    else:
        user['active'] = True
        flash(f'User {user["username"]} activated.', 'success')
    
    return redirect(url_for('admin.users'))


@admin.route('/user/<int:user_id>/deactivate')
def deactivate_user(user_id):
    """Deactivate a user."""
    # Find the user with the given ID
    user = next((u for u in users_data if u['id'] == user_id), None)
    
    if user is None:
        flash('User not found.', 'error')
    else:
        user['active'] = False
        flash(f'User {user["username"]} deactivated.', 'success')
    
    return redirect(url_for('admin.users'))


@admin.route('/user/<int:user_id>/delete')
def delete_user(user_id):
    """Delete a user."""
    # Find the user with the given ID
    user = next((u for u in users_data if u['id'] == user_id), None)
    
    if user is None:
        flash('User not found.', 'error')
    else:
        flash(f'User {user["username"]} deleted.', 'success')
        # In a real app, you would delete from the database
    
    return redirect(url_for('admin.users'))